//
//  SRPersonalDataViewController.m
//  SportRing
//
//  Created by icelee on 14-10-31.
//  Copyright (c) 2014年 HotOne. All rights reserved.
//

#import "SRPersonalDataViewController.h"
#import "SRPersonalDataView.h"
#import "SRAccountNetService.h"
#import "SRMe.h"
#import "SRMe+Cate.h"
#import "SRUserBLEService.h"
#import "UIImage+Ex.h"

@interface SRPersonalDataViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
    SRMe * me ;
}

@property (nonatomic, weak)IBOutlet SRPersonalDataView *srPersonalDataView;
@property (nonatomic, strong)UIImagePickerController *imagePicker;

@end

@implementation SRPersonalDataViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self addBackButton];
    
    me = [SRMe currentUser];
    [self setUserDefaultData];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self hideTabbar];
    self.navigationController.navigationBarHidden = NO;
}

#pragma mark - methods

- (void)setUserDefaultData
{
    SRPersonalDataViewModel *model = [[SRPersonalDataViewModel alloc] init];
    model.birthDay = me.birthday;
    model.weight = [me.weight integerValue];
    model.height = [me.height integerValue];
    model.nickname = me.nickName;
    
    if ([me.gender integerValue] == SRGenderFemale)
    {
        model.sexType = SR_SEX_TYPE_FAMALE;
    }
    if ([me.gender integerValue] == SRGenderMale)
    {
        model.sexType = SR_SEX_TYPE_MALE;
    }
    
    [_srPersonalDataView setSRPersonalDataViewData:model];
}

- (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize
{
    // Create a graphics image context
    UIGraphicsBeginImageContext(newSize);
    // Tell the old image to draw in this new context, with the desired
    // new size
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    // Get the new image from the context
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    // End the context
    UIGraphicsEndImageContext();
    // Return the new image.
    return newImage;
}

- (void)pickImageFromAlbum
{
    _imagePicker = [[UIImagePickerController alloc] init];
    _imagePicker.delegate = self;
    _imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    _imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    _imagePicker.allowsEditing = YES;
    [self presentViewController:_imagePicker animated:YES completion:nil];
}

- (void)pickImageFromCamera
{
    _imagePicker = [[UIImagePickerController alloc] init];
    _imagePicker.delegate = self;
    _imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    _imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    _imagePicker.allowsEditing = YES;
    [self presentViewController:_imagePicker animated:YES completion:nil];
    
}

#pragma mark - SRPersonalDataViewDelegate

- (void)didModifyHeader
{
    
}

- (void)didModifyNickname:(NSString *)nickname
{
    [SVProgressHUD show];
    
    [[SRMe currentUser] configAttributesWithBlock:^(SRMe *meSelf) {
        meSelf.nickName = nickname;
    }];
    
    [[SRAccountNetService updateProfileWithNickName:nickname
                                           birthDay:me.birthday
                                             weight:me.weight
                                             height:me.height
                                             gender:me.gender]
     subscribeNext:^(id x) {
         
         [SVProgressHUD dismiss];
         
     } error:^(NSError *error) {
         
         [SVProgressHUD showErrorWithStatus:@"修改个人昵称失败"];
     }];
}

- (void)didModifySex:(SR_SEX_TYPE )sexType
{
    SRGender gender;
    
    if (sexType == SR_SEX_TYPE_MALE)
    {
        gender = SRGenderMale;
    }
    if (sexType == SR_SEX_TYPE_FAMALE)
    {
        gender = SRGenderFemale;
    }
    
    [me configAttributesWithBlock:^(SRMe *meSelf) {
        meSelf.gender = @(gender);
    }];
    [SRUserBLEService updateMe:me];
    
    [SVProgressHUD show];
    
    [[SRAccountNetService updateProfileWithNickName:me.nickName
                                           birthDay:me.birthday ? me.birthday : @""
                                             weight:me.weight
                                             height:me.height
                                             gender:@(gender)]
     subscribeNext:^(id x) {
         [SVProgressHUD dismiss];
     } error:^(NSError *error) {
         [SVProgressHUD showErrorWithStatus:@"修改个人性别失败"];
     }];
}
- (void)didModifyBirthDay:(NSDate *)birthDay
{
    [me configAttributesWithBlock:^(SRMe *meSelf) {
        meSelf.birthday = [@([birthDay year]) stringValue];
    }];
    [SRUserBLEService updateMe:me];
    
    [SVProgressHUD show];
    [[SRAccountNetService updateProfileWithNickName:me.nickName
                                           birthDay:[birthDay formattedBy_yyyyMMdd]
                                             weight:me.weight
                                             height:me.height
                                             gender:me.gender]
     subscribeNext:^(id x) {
         [SVProgressHUD dismiss];
     } error:^(NSError *error) {
         [SVProgressHUD showErrorWithStatus:@"修改个人生日失败"];
     }];
}

- (void)didModifyWeight:(NSInteger)weight
{
    [me configAttributesWithBlock:^(SRMe *meSelf) {
        meSelf.weight = @(weight);
    }];
    [SRUserBLEService updateMe:me];
    
    [SVProgressHUD show];
    
    [[SRAccountNetService updateProfileWithNickName:me.nickName
                                           birthDay:me.birthday
                                             weight:[NSNumber numberWithLong:weight]
                                             height:me.height
                                             gender:me.gender]
     subscribeNext:^(id x) {
         [SVProgressHUD dismiss];
     } error:^(NSError *error) {
         [SVProgressHUD showErrorWithStatus:@"修改个人体重失败"];
     }];
}

- (void)didModifyHeight:(NSInteger)height
{
    [me configAttributesWithBlock:^(SRMe *meSelf) {
        meSelf.height = @(height);
    }];
    [SRUserBLEService updateMe:me];
    
    [SVProgressHUD show];
    
    [[SRAccountNetService updateProfileWithNickName:me.nickName
                                           birthDay:me.birthday
                                             weight:me.weight
                                             height:[NSNumber numberWithLong:height]
                                             gender:me.gender]
     subscribeNext:^(id x) {
         [SVProgressHUD dismiss];
         
     } error:^(NSError *error) {
         [SVProgressHUD showErrorWithStatus:@"修改个人身高失败"];
     }];
}

- (void)takePhoto
{
    [self pickImageFromCamera];
    
}

- (void)gotoAlbum
{
    [self pickImageFromAlbum];
}

#pragma mark - UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{}];
    
    UIImage *image= [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    
    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera)
    {
        // UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    }
    UIImage *scaledImage = [self imageWithImageSimple:image scaledToSize:CGSizeMake(40, 40)];
    
    [SVProgressHUD show];
    [[SRAccountNetService modifyUserHeadImg:[scaledImage data]]
     subscribeNext:^(id x) {
         [SVProgressHUD dismiss];
    }];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

@end
